﻿using HARTCalibrationTool.SerialPortManager;
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HARTCalibrationTool.Commands
{
    public enum TemperatureBoxCMDsType
    {
        eSetTemp = 0x01,
        eStartRunning = 0x02,
        eStopRunning = 0x03,
    }

    public abstract class TemperatureBoxCMDsBase : CommandsBase
    {
        public TemperatureBoxCMDsBase(SerialPortsBase serialPortManager, string commandName) : base(serialPortManager, commandName)
        { 
        
        }
        public override async Task ExecuteCMD()
        {
            await SendCMD();
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() => { });
        }

        protected abstract Task SendCMD();

        public override VerifyDataFrameStatus VerifyDataFrameIntegrity(byte[] data)
        {
            int len = data.Length;

            if (len > 2)
            {
                if ((data[len - 2] == 13) && (data[len - 1] == 10)) // "\r\n"
                {
                    return VerifyDataFrameStatus.eSuccess;
                }
            }

            return VerifyDataFrameStatus.eWaiting;
        }
    }

    public class TemperatureBoxCMDs : TemperatureBoxCMDsBase
    {
        private TemperatureBoxCMDsType _cmdType;
        private object[] _args;

        public TemperatureBoxCMDs(SerialPortsBase serialPortManager, string commandName, TemperatureBoxCMDsType cmd, object[] args) : base(serialPortManager, commandName)
        {
            _cmdType = cmd;
            _args = args;
        }

        protected override async Task SendCMD()
        {
            string cmdString = null;

            if (_cmdType == TemperatureBoxCMDsType.eStartRunning)
            {
                cmdString = "POWER,ON\r\n";
            }
            else if (_cmdType == TemperatureBoxCMDsType.eStopRunning)
            {
                cmdString = "POWER,OFF\r\n";
            }
            else if (_cmdType == TemperatureBoxCMDsType.eSetTemp)
            {
                string temperatureStr = (string)_args[0];

                float temperature;

                if (float.TryParse(temperatureStr, out temperature))
                {
                    if ((temperature > 100.0) || (temperature < -40.0))
                    {
                        SerialPortsBase.PrintMessage(temperature + " 温度范围超限！");
                        return;
                    }

                    cmdString = $"TEMP,S{temperatureStr}\r\n";
                }
                else
                {
                    SerialPortsBase.PrintMessage(temperatureStr + " 格式错误！");

                    return;
                }
            }

            SerialPortsBase.PrintMessage("--> " + cmdString);

            await SerialPortMager.SerialPortWriteLine(cmdString);
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() => {
                SerialPortsBase.PrintMessage("<-- " + Encoding.UTF8.GetString(data));
            });
        }

        public override void OnFailure()
        {

        }

        public override void OnSuccess()
        {

        }

        public override void OnTimeout()
        {

        }
    }

    public class AutoCaliTemperatureBoxCMDs : TemperatureBoxCMDs
    {
        private TemperatureBoxCMDsType _cmd;

        public AutoCaliTemperatureBoxCMDs(SerialPortsBase serialPortManager, string commandName, TemperatureBoxCMDsType cmd, object[] args) : base(serialPortManager, commandName, cmd, args)
        {
            _cmd = cmd;
        }

        public override async Task ParseResponse(byte[] data)
        {
            string responseStr = Encoding.UTF8.GetString(data);

            await Task.Run(() => {
                if (_cmd == TemperatureBoxCMDsType.eStartRunning)
                {
                    if (responseStr == "OK:POWER,ON\r\n")
                    {
                        CommandExeSuccess();
                        SetCurrentCMDResponseFinishedFlg();
                    }
                }
                else if (_cmd == TemperatureBoxCMDsType.eSetTemp)
                {
                    if (responseStr.Contains("OK:TEMP,S"))
                    {
                        CommandExeSuccess();
                        SetCurrentCMDResponseFinishedFlg();
                    }
                }

                MainUI.loggerDebugMsg("<--" + responseStr);
                SerialPortsBase.PrintMessage("<-- " + responseStr);
            });
        }
    }
}
